﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hont
{
    public class ATTask : IATTask, IExecuteEndHandle
    {
        object IATTask.AsyncReturnValue { get { return null; } }

        Action mOnTriggerResult;
        event Action IATTask.OnTriggerResult
        {
            add { mOnTriggerResult += value; }
            remove { mOnTriggerResult -= value; }
        }

        Action<IExecuteEndHandle> mExecuteAction;


        public ATTask(Action<IExecuteEndHandle> executeAction)
        {
            this.mExecuteAction = executeAction;
        }

        public void Finished()
        {
            if (mOnTriggerResult != null)
            {
                mOnTriggerResult();
            }
        }

        public void Execute()
        {
            mExecuteAction(this);
        }
    }

    public class ATTask<T> : IATTask, IExecuteEndHandle<T>
    {
        AsyncValue<T> mAsyncValue;

        object IATTask.AsyncReturnValue { get { return mAsyncValue; } }

        Action mOnTriggerResult;
        event Action IATTask.OnTriggerResult
        {
            add { mOnTriggerResult += value; }
            remove { mOnTriggerResult -= value; }
        }

        Action<IExecuteEndHandle<T>> mExecuteAction;


        public ATTask(Action<IExecuteEndHandle<T>> executeAction)
        {
            mAsyncValue = new AsyncValue<T>();
            this.mExecuteAction = executeAction;
        }

        public void Finished(T value)
        {
            if (mOnTriggerResult != null)
            {
                mAsyncValue.Value = value;
                mOnTriggerResult();
            }
        }

        public void Execute()
        {
            mExecuteAction(this);
        }
    }
}